//Data logger Demonstration using Stalker V2.1 Logs temperature to a file

void enableInterrupt()
{
    RTC.clearINTStatus(); //This function call is  a must to bring /INT pin HIGH after an interrupt.
    RTC.enableInterrupts(interruptTime.hour(),interruptTime.minute(),interruptTime.second());    // set the interrupt at (h,m,s)
    attachInterrupt(0, INT0_ISR, LOW);  //Enable INT0 interrupt (as ISR disables interrupt). This strategy is required to handle LEVEL triggered interrupt

}

void powerDown()
{
    cli(); 
    sleep_enable();      // Set sleep enable bit
    sleep_bod_disable(); // Disable brown out detection during sleep. Saves more power
    sei();
        
    DEBUG_PRINT("\nSleeping for " + String (sleepTime) + " seconds\n");
    delay(10); //This delay is required to allow print to complete
    //Shut down all peripherals like ADC before sleep. Refer Atmega328 manual
    power_all_disable(); //This shuts down ADC, TWI, SPI, Timers and USART
    sleep_cpu();         // Sleep the CPU as per the mode set earlier(power down)  

}

void awake()
{
    sleep_disable();     // Wakes up sleep and clears enable bit. Before this ISR would have executed
    power_all_enable();  //This shuts enables ADC, TWI, SPI, Timers and USART
    
    delay(10); //This delay is required to allow CPU to stabilize
    DEBUG_PRINT("Awake from sleep");    
    
    //Pequeño parpadeo del led de encendido para que sepamos que el cacharro está vivo
    ledBlink(POWER_LED_D_PIN, 1, 3, 200);

}

  
//Interrupt service routine for external interrupt on INT0 pin conntected to DS3231 /INT
void INT0_ISR()
{
  //Keep this as short as possible. Possibly avoid using function calls
    detachInterrupt(0); 
    interruptTime = DateTime(interruptTime.get() + sleepTime);  //decide the time for next interrupt, configure next interrupt  
}

